package com.shunan.miaosha.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shunan.miaosha.beans.OrderInfo;
import com.shunan.miaosha.beans.Users;
import com.shunan.miaosha.vo.GoodsVo;

@Service
public class MiaoshaService {
	
	@Autowired
	GoodsService goodsService;
	
	@Autowired
	OrderService orderService;
	
	/**
	 * 秒杀成功，那么返回秒杀订单信息，否则返回null
	 * @param user
	 * @param goods
	 * @return
	 */
	@Transactional
	public OrderInfo miaosha(Users user, GoodsVo goods) {
		//减库存 下订单 写入秒杀订单
		if(!goodsService.reduceStock(goods)){
			return null;  //若是没有秒杀成功，直接返回null
		}
		
		//order_info maiosha_order
		return orderService.createOrder(user, goods);
	}
	
}
